var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        commentData: [
            { title: '全部', num: 0, select: 0 },
            { title: '好评', num: 0, select: 1 },
            { title: '中评', num: 0, select: 2 },
            { title: '差评', num: 0, select: 3 },
        ],
        evaluationSelect: 0,
        evaluateList:{
          "list":[],
          "ecount":{
            "allnum":0,
            "goodsnum": 0,
            "generalnum": 0,
            "badnum": 0,
          }
        },
        praise_rate:0,
        evaluation_rate: 0,
        bad_rate: 0,
        goodsId:"",
        page:1,
        grade:""
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.setData({
        goodsId: options.id
      });
      this.getEvaluateList();
    },
    //下拉刷新
    onPullDownRefresh: function () {
      this.setData({
        evaluateList: {
          "list": [],
          "ecount": {
            "allnum": 0,
            "goodsnum": 0,
            "generalnum": 0,
            "badnum": 0,
          }
        },
        page: 1,
      });
      this.getEvaluateList();
    },
    //上拉加载
    onReachBottom: function () {
      this.getEvaluateList();
    },
    getEvaluateList: function () {
      var goodsId = this.data.goodsId;
      var that = this;
      var grade = this.data.grade;
      var data = {
        url: "/v1/goodsevaluate/lists",
        goods_id: goodsId,
        page: that.data.page,
        limit: 10,
        grade: grade
      };
      utils.request(data).then(function (res) {
        if (res.code == 0) {
          var data = res.data;
          if (data.list.length > 0) {
            that.setData({
              page: that.data.page + 1
            });
          }
          let new_data = that.data.evaluateList;
          if (data.list.length > 0){
            for (let i = 0; i < data.list.length;i++){
              new_data.list.push(data.list[i]);
            }
          }
          new_data.ecount = data.ecount;
          praise_rate = parseFloat((data.evaluateList.ecount.goodsnum+data.evaluateList.ecount.generalnum)/data.evaluateList.ecount.allnum) * 100;
          evaluation_rate = parseFloat(data.evaluateList.ecount.generalnum/data.evaluateList.ecount.allnum) * 100;//中评
          bad_rate = parseFloat(data.evaluateList.ecount.badnum/data.evaluateList.ecount.allnum)*100;//差评   
          that.setData({
            evaluateList: new_data,
            praise_rate: praise_rate,
            evaluation_rate: evaluation_rate,
            bad_rate: bad_rate
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
        wx.stopPullDownRefresh();
      });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    evaluationt: function (e) {
        let key = e.currentTarget.dataset.select;
        var grade = "";
        if (key == 1) {//好评
            grade = "4,5";
        } else if (key == 2) {
            grade = "3";
        } else if (key == 3) {
            grade = "1,2";
        }
        this.setData({
            evaluationSelect: key,
            grade: grade,
            evaluateList: {
              "list":[],
              "ecount": {
                "allnum": 0,
                "goodsnum": 0,
                "generalnum": 0,
                "badnum": 0,
              }
            },
            page: 1,
        })
        this.getEvaluateList();
    },
})